package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @param
 * @return
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    NursingPlanMapper nursingPlanMapper;

    @Autowired
    NursingProjectPlanMapper nursingProjectPlanMapper;

    //分页查询所有数据
    @Override
    public PageResponse selectByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> nursingPlanVoPage = nursingPlanMapper.selectByPage(name, status);
        PageResponse<NursingPlanVo> of = PageResponse.of(nursingPlanVoPage, NursingPlanVo.class);
        of.getRecords().forEach(vo -> {
            //查询count数量
            Long count = nursingPlanMapper.selectPlanGradeCount(vo.getId());
            vo.setCount(count);
        });
        return of;
    }

    //新增护理计划
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertNursingPlan(NursingPlanDto nursingPlanDto) {
        //将nursingPlanDto 转为nursingPlan对象
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(1);
        //nursingPlanDto.setCreateBy(1L);
        //插入护理计划的相关数据
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        //插入到中间表nursing_project_plan
        List<NursingProjectPlan> projectPlans = nursingPlan.getProjectPlans();
        if (projectPlans != null && !projectPlans.isEmpty()) {
            //通过@Options拿到的id将其赋值给javaBean(NursingProjectPlan)的planId属性
            projectPlans.forEach(s -> s.setPlanId(nursingPlan.getId()));
            nursingProjectPlanMapper.insertProjectPlan(projectPlans);
        }
    }

    //查询回显
    @Override
    public NursingPlanVo getByIdPlan(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.getByIdPlan(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        return nursingPlanVo;
    }

    //修改护理计划
    @Override
    public void selectNursingPlan(Long id, NursingPlanDto nursingPlanDto) {
        //将NursingPlanDto转为NursingPlan对象
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setId(id);
        //修改NursingPlan的数据
        nursingPlanMapper.nursingPlanMapper(nursingPlan);
        //先删除中间表的护理计划
        nursingProjectPlanMapper.deleteByIds(Arrays.asList(id));
        //然后再将数据插入到中间表
        List<NursingProjectPlan> projectPlans = nursingPlan.getProjectPlans();
        if (projectPlans != null && !projectPlans.isEmpty()) {
            //通过@Options拿到的id将其赋值给javaBean(NursingProjectPlan)的planId属性
            projectPlans.forEach(s -> s.setPlanId(id));
            nursingProjectPlanMapper.insertProjectPlan(projectPlans);
        }
    }

    //删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteNursingPlan(Long id) {
        try {
            //删除护理计划
            nursingPlanMapper.deleteNursingPlan(id);
            //删除中间表的数据
            nursingProjectPlanMapper.deleteById(id);
        } catch (Exception e) {
            //关联了中间表删不了
            e.printStackTrace();
        }
    }

    //是否禁用
    @Override
    public void isNursingPlan(Long id, Integer status) {
        nursingPlanMapper.isNursingPlan(id, status);
    }

    //查询所有护理计划
    @Override
    public List<NursingPlanVo> selectALLNusingPlan() {
        return nursingPlanMapper.selectALLNusingPlan();
    }
}
